//
//  MyAdressViewController.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/28.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "MyAdressViewController.h"
#import "MyAdressViewModel.h"
#import "AddAdressViewController.h"
#import "MyAdressModel.h"

@interface MyAdressViewController ()<MyAdressActionDelegate>{
    MyAdressModel * adressModek;
}
@property (nonatomic,strong) MyAdressViewModel * myAdressViewModel;
@end

@implementation MyAdressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"地址管理";
    self.myAdressViewModel = [[MyAdressViewModel alloc] initWithViewToViewModel:self.tableView theSuperView:self.view];
    self.myAdressViewModel.delegate = self;
    [self headerRereshing];
}

-(void)headerRereshing{
    self.page = 1;
    [self loadData];
}

-(void)footerRereshing{
    if (self.page < adressModek.data.pages) {
        self.page ++;
        [self loadData];
    }else{
        [GLGWProgressHUD showNotice:@"没有更多地址了！" view:nil];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }
}

-(void)loadData
{
    @weakify(self)
    [[self.myAdressViewModel.recommendCommand execute:[NSNumber numberWithInteger:self.page]]subscribeNext:^(id x) {
        @strongify(self)
        self->adressModek = (MyAdressModel *)x;
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }error:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];

    }];
}

#pragma mark -- MyAdressActionDelegate
-(void)adressAddAction{
    AddAdressViewController * adressVC = [AddAdressViewController new];
    adressVC.Block_CaoZuoAccount = ^(AddOrEditType addType) {
        [self headerRereshing];
    };
    [self.navigationController pushViewController:adressVC animated:YES];
}

-(void)editAdressInforModel:(MyAdressModel_Infos *)e_Model{
    if (self.BlockChooseAdrees) {
        self.BlockChooseAdrees(e_Model);
        return;
    }
    AddAdressViewController * editAdressVC = [AddAdressViewController new];
    editAdressVC.adressInfor = e_Model;
    editAdressVC.Block_CaoZuoAccount = ^(AddOrEditType addType) {
        [self headerRereshing];
    };
    [self.navigationController pushViewController:editAdressVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
